package com.lenovo.smarttraffic.ui.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.DatePicker;

import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.api.onDateChoose;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;


/**
 * Created by Caleb.F
 * on 2020-01-14
 * at 2:08
 */
public class CalendarDialog extends DialogFragment {
    private static final String TAG = "DateDialogFragment";
    private Button mBtnCacel;
    private Button mBtnChoose;
    private Button mBtnConfirm;
    private DatePicker mMdatePicker;
    private onDateChoose mOnDateChoose;
    private String mDateString;
    private static final String CALENDAR_DIALOG = "com.lenovo.smarttraffic.ui.fragment.CalendarDialog";

    public static CalendarDialog newInstance(String argument) {
        Bundle bundle = new Bundle();
        bundle.putString(CALENDAR_DIALOG, argument);
        CalendarDialog calendarDialog = new CalendarDialog();
        calendarDialog.setArguments(bundle);
        return calendarDialog;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDateString=getArguments().getString(CALENDAR_DIALOG );
    }

    @Override
    public void onStart() {
        super.onStart();
        getDialog().getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        return inflater.inflate(R.layout.calendar_dialog, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    private void initView(View view) {
        mBtnCacel = view.findViewById(R.id.btn_cacel);
        mBtnChoose = view.findViewById(R.id.btn_choose);
        mBtnConfirm = view.findViewById(R.id.btn_confirm);
        mMdatePicker = view.findViewById(R.id.mdatePicker);
        mMdatePicker.setEnabled(true);
        int year = Integer.parseInt(mDateString.split("-")[0]);
        int month = Integer.parseInt(mDateString.split("-")[1]);
        int day = Integer.parseInt(mDateString.split("-")[2]);
        //设置日历控件的初始化,不可使用set方法，该方法会需要不同的API
        mMdatePicker.init(year, month-1, day, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

            }
        });
        mBtnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int year = mMdatePicker.getYear();
                int month = mMdatePicker.getMonth();
                int dayOfMonth = mMdatePicker.getDayOfMonth();
                StringBuilder stringBuilder = new StringBuilder();
                String s = stringBuilder.append(year).append("-").append(month + 1).append("-").append(dayOfMonth).toString();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
                try {
                    Date parse = simpleDateFormat.parse(s);
                    mOnDateChoose.getDate(parse);
                } catch (ParseException e) {
                }
                dismiss();
            }
        });
        mBtnCacel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }


    public void setonDateChoose(onDateChoose onDateChoose) {
        mOnDateChoose = onDateChoose;
    }

}
